﻿#include "page_info_2.h"
#include "ui_page_info_2.h"
#include "net_data.h"
#include "net_mgr.h"

#include <mhelper.h>



#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif



PageInfo_2::PageInfo_2(QWidget* parent)
	: QWidget(parent)
    , ui(new Ui::PageInfo_2)
	, curIndex(-1)
{
	ui->setupUi(this);

	model_data = new QStandardItemModel;
	ui->tableView->setModel(model_data);

	ui->tableView->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
	ui->tableView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
	ui->tableView->setEditTriggers(QTableView::NoEditTriggers);

	ui->tableView->setAlternatingRowColors(true);

	QStringList header_label;
	header_label << "节点ID" << "节点类型" << "节点值" << "频率" << "相位偏移";
	model_data->setHorizontalHeaderLabels(header_label);
	model_data->setRowCount(156);

	ui->tableView->setColumnWidth(0, 80);
	ui->tableView->setColumnWidth(1, 100);
	ui->tableView->setColumnWidth(2, 120);
	ui->tableView->setColumnWidth(3, 80);
	ui->tableView->setColumnWidth(4, 80);

	init_tableview();
	//  ui->tableView->>setHorizontalHeaderLabels()->set;
	//  model_data ->set>set>horizaltalheader()->header
	init_graph();
    
    
    connect(NetMgr::upd_server, &UDPMaster::sigNewData, [=]
		{
			update_tableview();

			if (curIndex == -1)
				return;
			updateGraph();


			//update tableview 
		});
	//ui->widget->replot();
}

PageInfo_2::~PageInfo_2()
{
	delete ui;
}

void PageInfo_2::init_tableview()
{
	NetData* netData = NetData::Instance();

	QVector<LowerNode*>& lower_data = netData->lower_data;
	for (int row = 0; row < lower_data.count(); row++)
	{
		LowerNode* lower = lower_data[row];

		QModelIndex index_0 = model_data->index(row, 0);
		model_data->setData(index_0, lower->name_id);

		QModelIndex index_1 = model_data->index(row, 1);
		model_data->setData(index_1, lower->type);

	}
}

void PageInfo_2::init_graph()
{
	//ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    int graphCount = ui->widget->graphCount();
//	if (graphCount == 0)
		//ui->widget->addGraph();
//		ui->widget->AddNewPlot("", QVector<double>(), QVector<double>());

	//ui->widget->rescaleAxes();
	ui->widget->xAxis->setRange(0, 1000);
	ui->widget->yAxis->setRange(-100, 100);
    ui->widget->yAxis->setVisible(true);
}

void PageInfo_2::update_tableview()
{
	NetData* netData = NetData::Instance();

	QVector<LowerNode*>& lower_data = netData->lower_data;

	for (int row = 0; row < lower_data.count(); row++)
		//	while (it != lower_data.end())
	{
        LowerNode* lower = lower_data[row];
		QModelIndex index_2 = model_data->index(row, 2);
		model_data->setData(index_2, lower->Value.last(), Qt::DisplayRole);
		model_data->setData(index_2, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);


		if (lower->Frequency.count() > 0)
		{
			QModelIndex index_3 = model_data->index(row, 3);
			model_data->setData(index_3, lower->Frequency.last());
		}
		if (lower->Phase.count() > 0)
		{
			QModelIndex index_4 = model_data->index(row, 4);


			model_data->setData(index_4, lower->Phase.last());
		}
	}

}

void PageInfo_2::updateGraph()
{
	///update chart
	QVector<LowerNode*>& lower_data = NetData::Instance()->lower_data;
	const	LowerNode* lower_0 = lower_data.at(curIndex);

	int cnt = lower_0->Value.count();

    int graphCount = ui->widget->graphCount();
    if (ui->widget->graphCount() == 0)
        ui->widget->AddNewPlot("", QVector<double>(), QVector<double>());


	ui->widget->graph(0)->addData((double)cnt, lower_0->Value.last());
	ui->widget->graph(0)->setPen(QPen(Qt::white));
    int x_start = cnt > 500 ? cnt - 500 : 0;

	//ui->widget->rescaleAxes();
	ui->widget->xAxis->setRange(x_start, x_start + 1000);
	ui->widget->replot();
}

void PageInfo_2::on_tableView_doubleClicked(const QModelIndex& index)
{
	curIndex = index.row();

	LowerNode* node = NetData::Instance()->lower_data.at(curIndex);

	QVector<qreal> x;// = node.Value;
	for (int i = 0; i < node->Value.count(); i++)
		x << i;

	QVector<qreal> y = node->Value;

	int graphCount = ui->widget->graphCount();

	if (graphCount == 0)
    //	ui->widget->addGraph();
    ui->widget->AddNewPlot("", QVector<double>(), QVector<double>());

	ui->widget->graph(0)->setData(x, y);
	//ui->widget->rescaleAxes();
	ui->widget->replot();


}

